﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.门店出入库.Service;
using DXApplicationTangche.Utils;
using DXApplicationTangche.DTO;
using System.IO;
using DXApplicationTangche.UC.成品出库单.service;
using DXApplicationTangche.UC.门店出入库.UC;
using Seagull.BarTender.Print;

namespace DXApplicationTangche.UC.标签打印
{
    public partial class Form库存质检贴 : DevExpress.XtraEditors.XtraForm
    {
        private List<StockInOutDetailDTO> stockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private StockInOutDetailDTO stockInOutDetail = new StockInOutDetailDTO();
        private List<StockInventoryDao> stockInventoryDaos = new List<StockInventoryDao>();
        private List<StockInventoryDao> stockInventoryDaos出库 = new List<StockInventoryDao>();
        //  库存明细
        private XtraUserControl库存明细 xtraUserControl;
        public Form库存质检贴()
        {
            InitializeComponent();
            this.xtraUserControl = new XtraUserControl库存明细();
            this.xtraUserControl.Dock = DockStyle.Fill;
            this.panel库存.Controls.Clear();
            this.panel库存.Controls.Add(this.xtraUserControl);
        }

        private void textBox水洗_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox水洗.Text))
                {
                    try
                    {
                        //  出库
                        this.stockInOutDetail = 出入库Service.factoryOtherSaleOut(this.textBox水洗.Text.Trim());
                        //  尺码清洗
                        try
                        {
                            if (!String.IsNullOrEmpty(this.stockInOutDetail.styleSizeCd))
                            {
                                this.stockInOutDetail.styleSizeCd = this.stockInOutDetail.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", "");
                            }
                        }
                        catch { }
                        //  打印出库贴
                        //this.print出库合格证();
                        //  gridcontrol显示
                        if (!String.IsNullOrEmpty(this.stockInOutDetail.barcode))
                        {
                            if (this.stockInOutDetailDTOs.Count > 0)
                            {
                                if (!this.stockInOutDetailDTOs.Exists(dto => dto.barcode.Equals(this.stockInOutDetail.barcode)))
                                {
                                    this.stockInOutDetailDTOs.Add(this.stockInOutDetail);
                                }
                            }
                            else
                            {
                                this.stockInOutDetailDTOs.Add(this.stockInOutDetail);
                            }
                        }
                        //  
                        List<StockShortDTO> stockShortDTOs = new List<StockShortDTO>();
                        foreach (var stockInOutDetailDTO in this.stockInOutDetailDTOs)
                        {
                            stockShortDTOs.Add(new StockShortDTO(stockInOutDetailDTO));
                        }
                        this.pivotGridControl1.DataSource = stockShortDTOs;
                        this.pivotGridControl1.RefreshData();
                        this.pivotGridControl1.BestFitColumnArea();
                        this.pivotGridControl1.BestFitRowArea();
                        //this.gridControl出库一览.DataSource = this.stockInOutDetailDTOs;
                        //this.gridView出库一览.RefreshData();
                        //this.gridView出库一览.BestFitColumns();
                        this.textBox水洗.Clear();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }

        private void Form入库_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.textBox水洗;
            Form出入库 Form入库 = new Form出入库();
            Form入库.TopLevel = false;
            Form入库.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            Form入库.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Controls.Add(Form入库);
            Form入库.Show();
        }

        
        private void simpleButton出库导出_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出抖音订单";
            fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.pivotGridControl1.ExportToXlsx(fileDialog.FileName);
                //this.gridView出库一览.ExportToXlsx(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("文件已保存至" + fileDialog.FileName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void simpleButton更新模板_Click(object sender, EventArgs e)
        {
            string saveDirectoryPath = @"tag\";
            //获取不同模板
            String filePath入库模板 = "stock_qcpass_out.btw";
            String fileName入库模板 = Path.GetFileName(filePath入库模板);
            //根据相对路径判断模板是否存在
            //存在则删除
            if (System.IO.File.Exists(saveDirectoryPath + fileName入库模板))
            {
                File.Delete(saveDirectoryPath + filePath入库模板);
            }
        }


        #region
        private void simpleButton打印流转贴_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                String stockBarcode = 出入库Service.gerenateStockBarcode();
                this.print流转码(stockBarcode);
            }
        }
        private void print流转码(String stockBarcode93)
        {
            //留给销售的大吊牌
            //根据类型获取不同模板
            String filePath流转码 = "stockBarcode93.btw";
            String fileName流转码 = Path.GetFileName(filePath流转码);
            //根据相对路径判断模板是否存在
            //不存在则下载进行打印
            if (!System.IO.File.Exists(@"tag\" + fileName流转码))
            {
                FileUtil.DownloadPicture(URL.图片url + URL.打印模板路径 + filePath流转码, @"tag\" + fileName流转码, -1);
            }
            String str = System.AppDomain.CurrentDomain.BaseDirectory;
            Engine btEngine = new Engine();
            btEngine.Start();
            #region print补打大吊牌
            LabelFormatDocument btFormat = btEngine.Documents.Open(str + @"tag\" + fileName流转码);
            //指定打印机名 
            //btFormat.PrintSetup.PrinterName = "Microsoft Print to PDF";
            btFormat.PrintSetup.PrinterName = "POSTEK G-3106";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 1;
            //对BTW模版相应字段进行赋值 
            btFormat.SubStrings["stockBarcode93"].Value = stockBarcode93;

            Messages messages;
            int waitout2 = 10000; // 10秒 超时 
            Result nResult2 = btFormat.Print("流转码" + stockBarcode93, waitout2, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
            #endregion
        }

        private void textBox成衣码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox流转码.Text))
                {
                    MessageBox.Show("请先扫描流转码");
                    this.ActiveControl = textBox流转码;
                    this.textBox成衣码.Clear();
                }
                if (!String.IsNullOrWhiteSpace(this.textBox成衣码.Text))
                {
                    try
                    {
                        //  入库
                        StockInventoryDao stockInventoryDao =
                            出入库Service.barcodeStockIn(this.textBox流转码.Text.Trim(), this.textBox成衣码.Text.Trim());
                        try
                        {
                            if (!String.IsNullOrEmpty(stockInventoryDao.size))
                            {
                                stockInventoryDao.size = stockInventoryDao.size.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", "");
                            }
                        }
                        catch { }
                        //  gridcontrol显示
                        //if (!String.IsNullOrEmpty(stockInventoryDao.barcode))
                        //{
                        //    if (this.stockInventoryDaos.Count > 0)
                        //    {
                        //        if (!this.stockInventoryDaos.Exists(dto => dto.barcode.Equals(stockInventoryDao.barcode)))
                        //        {
                        //            this.stockInventoryDaos.Add(stockInventoryDao);
                        //        }
                        //    }
                        //    else
                        //    {
                        this.stockInventoryDaos.Add(stockInventoryDao);
                        //    }
                        //}
                        this.gridControl入库.DataSource = this.stockInventoryDaos;
                        this.gridView入库.RefreshData();
                        this.gridView入库.BestFitColumns();
                        this.textBox流转码.Clear();
                        this.textBox成衣码.Clear();
                        this.ActiveControl = this.textBox流转码;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private void textBox流转码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.ActiveControl = this.textBox成衣码;
            }
        }

        private void textBox出库扫流转码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                StockInventoryDao stockInventoryDao出库 = 出入库Service.barcodeStockOut(this.textBox出库扫流转码.Text.Trim());
                if (!String.IsNullOrEmpty(stockInventoryDao出库.stockbarcode))
                {
                    //if (this.stockInventoryDaos出库.Count > 0)
                    //{
                    //    if (!this.stockInventoryDaos出库.Exists(dto => dto.barcode.Equals(stockInventoryDao出库.barcode)))
                    //    {
                    //        this.stockInventoryDaos出库.Add(stockInventoryDao出库);
                    //    }
                    //}
                    //else
                    //{
                    try
                    {
                        if (!String.IsNullOrEmpty(stockInventoryDao出库.size))
                        {
                            stockInventoryDao出库.size = stockInventoryDao出库.size.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", "");
                        }
                    }
                    catch { }
                    this.stockInventoryDaos出库.Add(stockInventoryDao出库);
                    //}
                    this.gridControl流转码出库.DataSource = this.stockInventoryDaos出库;
                    this.gridView流转码出库.RefreshData();
                    this.textBox出库扫流转码.Clear();
                }
            }
        }
        #endregion

        private void textBox输入成衣码查看商家编码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox输入成衣码查看商家编码.Text.Trim()))
                {
                    String spec = 出入库Service.getSpecNoByBarcode(this.textBox输入成衣码查看商家编码.Text.Trim());
                    this.textBox查看商家编码.Text = spec;
                }
            }
        }

        private void simpleButton查看每日销售明细_Click(object sender, EventArgs e)
        {
            this.gridControl每日销售出库明细.DataSource = 出入库Service.getStockSpecAll();
            this.gridView每日销售出库明细.RefreshData();
        }

        //private void textBox入库水洗_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyValue == 13)
        //    {
        //        if (!String.IsNullOrWhiteSpace(this.textBox入库水洗.Text))
        //        {
        //            try
        //            {
        //                //  入库
        //                this.tagPrint = 出入库Service.garmentWipFactoryStockIn(this.textBox入库水洗.Text.Trim());
        //                try
        //                {
        //                    if (!String.IsNullOrEmpty(this.tagPrint.styleSizeCd))
        //                    {
        //                        this.tagPrint.styleSizeCd = this.tagPrint.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", "");
        //                    }
        //                }
        //                catch { }
        //                //  打印入库贴
        //                //this.print质检合格证();
        //                //  gridcontrol显示
        //                if (!String.IsNullOrEmpty(this.tagPrint.inventoryNo))
        //                {
        //                    if (this.tagPrintDTOs.Count > 0)
        //                    {
        //                        if (!this.tagPrintDTOs.Exists(dto => dto.inventoryNo.Equals(this.tagPrint.inventoryNo)))
        //                        {
        //                            this.tagPrintDTOs.Add(this.tagPrint);
        //                        }
        //                    }
        //                    else
        //                    {
        //                        this.tagPrintDTOs.Add(this.tagPrint);
        //                    }
        //                }
        //                this.gridControl入库一览.DataSource = this.tagPrintDTOs;
        //                this.gridView入库一览.RefreshData();
        //                this.gridView入库一览.BestFitColumns();
        //                this.textBox入库水洗.Clear();
        //            }
        //            catch (Exception ex)
        //            {
        //                MessageBox.Show(ex.Message);
        //            }
        //        }
        //        else
        //        {
        //            MessageBox.Show("请扫码");
        //        }
        //    }
        //}
        //private void simpleButton打印模板更新_Click(object sender, EventArgs e)
        //{
        //    string saveDirectoryPath = @"tag\";
        //    //获取不同模板
        //    String filePath入库模板 = "stock_qcpass.btw";
        //    String fileName入库模板 = Path.GetFileName(filePath入库模板);
        //    //根据相对路径判断模板是否存在
        //    //存在则删除
        //    if (System.IO.File.Exists(saveDirectoryPath + fileName入库模板))
        //    {
        //        File.Delete(saveDirectoryPath + filePath入库模板);
        //    }
        //}
    }
}